<?php
namespace App\HttpController;

use EasySwoole\Http\AbstractInterface\AbstractRouter;
use EasySwoole\Http\Request;
use EasySwoole\Http\Response;
use FastRoute\RouteCollector;
use EasySwoole\EasySwoole\Config;

class Router extends AbstractRouter {

    public function initialize(RouteCollector $routeCollector) {
        $urlRouteOn = Config::getInstance()->getConf('web.url_route_on');
        // if (empty($urlRouteOn)) {
        //     $this->setGlobalMode($urlRouteOn);
        // }
        $configs = Config::getInstance()->getConf('web.router');
        foreach ($configs as $config){
            $routeCollector->addRoute(strtoupper($config[0]), $config[1], $config[2]);
        }
        // $this->setMethodNotAllowCallBack(function (Request $request, Response $response) use ($urlRouteOn) {
        //     if ($urlRouteOn) {
        //         $response->write('not allow');
        //         return false;//结束此次响应
        //     }
        //     return true;
        // });
        $this->setRouterNotFoundCallBack(function (Request $request, Response $response) use ($urlRouteOn) {
            if ($urlRouteOn) {
                $response->write('miss router.');
                return false;
            }
            return true;
        });
    }
}
